﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaixin.Components.InterFace;
using System.Threading;
using Kaixin.Components.XML;
using Kaixin.Components.Master.BLL;
using Kaixin.Components.Master.DTO;

namespace Kaixin.Components.Master
{
    class MasterComponent:ComponentsBase
    {

        private IUIFactory _uif = new UIFactory();
        private Thread _workThread;
        private UserSet _us = new UserSet();
        private List<MasterRobot> _robotList = new List<MasterRobot>();

        public MasterComponent()
        {
            foreach (var user in _us.GetAllItem())
            {
                MasterRobot mr = new MasterRobot(user.Email, user.Password);
                _robotList.Add(mr);
            }
            _workThread = new Thread(work);
            _workThread.Name = "大师PK线程";
            _workThread.Start();
        }

        private void work()
        {
            while (true)
            {
                foreach (var robot in _robotList)
                {
                    foreach (var item in robot.GetMyOpponents().OrderBy(i => i.Level))
                    {
                        if (!item.IsDisciple && !item.IsTeacher)
                        {
                            //不与师傅或徒弟PK
                            PKResult result = robot.PK(item);
                            if (result == PKResult.TheLackOfTime)
                            {
                                break;
                            }
                            Thread.Sleep(5000);//休息5秒
                        }
                    }
                }
                Thread.Sleep(1000 * 60 * 10);
            }
        }

        public override List<string> GetMenus()
        {
            List<string> menu = new List<string>();
            menu.Add("大师信息");
            return menu;
        }

        public override Kaixin.Components.InterFace.IUIFactory UIFactory
        {
            get { return _uif; }
        }

        public override string Name
        {
            get { return "大师"; }
        }

        public override System.Threading.Thread BackgroundThread
        {
            get { return _workThread; }
        }

    }
}
